/*
 * AllwinnerTech hdmi cec driver
 *
 * Copyright (C) 2015 AllwinnerTech Corporation
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#ifndef __hdmi_cec_h__
#define __hdmi_cec_h__

#define CEC_EOM_BLOCK_INVALID	(-1)
#define CEC_EOM_BLOCK_MORE		(0)
#define CEC_EOM_BLOCK_END		(1)

/* timing define for cec send */
#define CEC_PER_SEND_FRAME		(500000)
#define CEC_START_BIT_LOW_TIME	(3700000)
#define CEC_START_BIT_HIGH_TIME	(800000)

#define CEC_BIT_WHOLE_TIME		(2400000)
#define CEC_LOGIC_0_LOW_TIME	(1500000)
#define CEC_LOGIC_1_LOW_TIME	(600000)

#define CEC_LOGIC_0_HIGH_TIME	(CEC_BIT_WHOLE_TIME - CEC_LOGIC_0_LOW_TIME)
#define CEC_LOGIC_1_HIGH_TIME	(CEC_BIT_WHOLE_TIME - CEC_LOGIC_1_LOW_TIME)

#define	CEC_BYTE_ACK_SAMPLE_TIME	(400000)
#define CEC_BYTE_ACK_COMPLETE_TIME \
	(CEC_BIT_WHOLE_TIME - CEC_LOGIC_1_LOW_TIME - CEC_BYTE_ACK_SAMPLE_TIME)

#define CEC_START_BIT_TOTAL_MAX		(4800000)
#define CEC_START_BIT_TOTAL_MIN		(4000000)

/* timing define for cec receive */
#define CEC_LOGICAL_BIT_SAMPLE_TIME	(1000000)
#define CEC_RECV_ASSERT_ACK_TIME	(1500000)

enum send_frame_state {
	SEND_FRAME_IDLE,
	SEND_FRAME_BUSY_DET,
	SEND_FRAME_START_BIT,
	SEND_FRAME_HEADER_BLOCK,
	SEND_FRAME_DATA_BLOCK_PRE,
	SEND_FRAME_DATA_BLOCK,
	SEND_FRAME_END
};

enum receive_frame_state {
	RECV_FRAME_IDLE,
	RECV_FRAME_HEADER_BLOCK,
	RECV_FRAME_DATA_BLOCK_PER,
	RECV_FRAME_DATA_BLOCK,
	RECV_FRAME_END
};

enum send_block_state {
	CEC_BIT_TR_PRE,
	CEC_BIT_TR_LOW,
	CEC_BIT_TR_HIGH,
	CEC_BIT_TR_ACK,
	CEC_BIT_TR_ACK_SAMPLE,	
};

enum receive_block_state {
	CEC_RECV_BIT_SAMPLE,
	CEC_RECV_EOM_SAMPLE,
	CEC_RECV_ASSERT_ACK,
};

/* error define */
#define	CEC_TRANSMIT_OK			(0)
#define CEC_TRANSMIT_NO_INPUT	(1)
#define CEC_TRANSMIT_LINE_BUSY	(2)
#define CEC_TRANSMIT_ACK_ERROR	(3)
#define CEC_TRANSMIT_ADDRESS_ERROR	(4)
#define	CEC_TRANSMIT_UNKNOW_ERROR	(5)

/* next transmit free time */
#define CEC_DEFAULT_PERIOD		(7)

#define CEC_MAKE_HEADER(_src_la, _dst_la) \
	(((_src_la<<4) & 0xf0) | (_dst_la & 0x0f))
#define CEC_GET_INITIATOR(_byte)	((_byte>>4) & 0x0f)
#define CEC_GET_DESTINATION(_byte)	(_byte & 0x0f)

#define PHY_ADDR_TYPE_ROOT		0
#define PHY_ADDR_TYPE_LOCAL		1
#define PHY_ADDR_TYPE_ACTIVE	2

#if 1
#define _CEC_INFO  (1<<0)
#define _CEC_DEBUG (1<<1)

#define cec_info( format, args... )			\
do {										\
    if(unlikely(debug_mask&_CEC_INFO)){		\
        printk(KERN_INFO format, ##args);	\
    }										\
}while(0)

#define cec_debug( format, args... )		\
do {										\
    if(unlikely(debug_mask&_CEC_DEBUG)){	\
        printk(KERN_ERR format, ##args);	\
    }										\
}while(0)

#else

#define cec_info( format, args... )
#define cec_debug( format, args... )

#endif

#define cec_err( format, args... )			\
do {										\
    printk(KERN_ERR format, ##args);		\
}while(0)

struct hdmi_cec_frame {
	int initiator;
	int destination;
	/* total of opcode and operands */
	int operand_num;
	/* operand[0] is the opcode
     * the remain is operands */
	int operand[16];
};

/* cec line operation */
int hdmi_cec_line_init(int portid);
void config_cec_line_as_input(void);
void config_cec_line_as_output(void);
void set_cec_line_state(int level);
int get_cec_line_state(void);
void cec_line_irq_disable(void);
void cec_line_irq_enable(void);

/* cec utils functions */
int hdmi_cec_utils_init(void);
int hdmi_cec_utils_exit(void);

/* cec core functions */
int is_logical_addr_match(int la);
int is_broadcast(int la);
int cec_get_physical_address(int type);
void cec_standby_broadcast(void);
void cec_wakeup_broadcast(void);
int cec_get_phy_addr_from_edid(void);
void cec_polling_message(void);
int  cec_core_init(void);
void cec_core_exit(void);
void cec_core_reset(void);

#endif
